package com.straw.hat.common.collection;

import java.util.Arrays;
import java.util.HashMap;

public class FunctionList {


    public static void main(String[] args) {

//        System.out.println(Arrays.toString(twoSum(new int[] {2,7,11,15},9)));
        System.out.println(Long.MAX_VALUE);
        System.out.println(Integer.MAX_VALUE);

//        System.out.println(subarraySum(new int[] {1,1,1},2));
    }


    public static int[] twoSum(int[] nums, int target) {
        int len = nums.length;
        for (int i = 0 ; i < len ; i++) {
            for(int j = i+1 ; j < len  ; j++){
                if ( nums[i] == target-nums[j] ) {
                    return new int[] { i, j };
                }
            }
        }
        throw new IllegalArgumentException("No two sum solution");

    }


    public static int subarraySum(int[] nums, int k) {
//        if (nums == null || nums.length == 0) return 0;
//        //dp[i]表示前i个数的和
//        int[] dp = new int[nums.length + 1];
//        for (int i = 1; i <= nums.length; i++) {
//            dp[i] = dp[i - 1] + nums[i - 1];
//        }
//        int ret = 0;
//        HashMap<Integer, Integer> map = new HashMap<>();
//        for (int i = 0; i < dp.length; i++) {
//            if (map.containsKey(dp[i] - k))
//                ret += map.get(dp[i] - k);
//            map.put(dp[i], map.getOrDefault(dp[i], 0) + 1);
//        }
//        System.out.println(map);
//        return ret;
        int len = nums.length;
        //累和元素
        int [] rt = new int [len+1];
        for (int i = 1; i <= len; i++) {
            rt [i] = rt [i-1] + nums[i-1];
        }
        HashMap<Integer,Integer> map = new HashMap();
        int res = 0;
        for (int i = 0 ; i < rt.length ; i++) {
            if (map.containsKey(rt[i]-k)) res += map.get(rt[i]-k);
            map.put(rt[i],map.getOrDefault(rt[i],0)+1);
        }
        return res;
    }



}
